<?php

/**
 * JingYao-backend
 *
 * @link     https://gitee.com/wang-zhihui-release/jingyao-backend
 * @apiDocument https://gitee.com/wang-zhihui-release/jingyao-backend/wikis/
 */

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * Class Article.
 * @property int identity_visibility
 * @property int type
 * @property string title
 * @property string content
 */
class Article extends Model
{
    use HasFactory;
    use SoftDeletes;

    protected $guarded = [];

    protected $casts = [
        'created_at' => 'date:Y-m-d H:i:s',
        'updated_at' => 'date:Y-m-d H:i:s',
        'push_time' => 'date:Y-m-d H:i:s',
    ];

    public function getDownloadPathAttribute($value)
    {
        if (empty($value)) {
            return '';
        }
        if (substr($value, 0, 4) == 'http') {
            return $value;
        }
        return config('app.url') . '/' . 'download?path=' . $value;
    }

    public function getCoverUrlAttribute($value)
    {
        if (empty($value)) {
            return '';
        }
        if (substr($value, 0, 4) != 'http') {
            return config('app.url') . $value;
        }
        return $value;
    }

    public function image()
    {
        return $this->hasOne(Images::class, 'id', 'background_image');
    }
}
